/*
 *   Copyright (C) 2007 by Andrzej Oczkowicz
 *   andrew_lz@poczta.fm
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *   KMediaCatalog :)
 *
 */

#include "kmaster.h"
#include "dbg.h"
#include "ksearch.h"
#include "kmedia.h"
#include "kmodmgr.h"
#include "kmediacatalog.h"
#include "kconfig.h"

kMaster::kMaster(QObject *parent)
 : QObject(parent)
{
  mainWnd = 0;
  modmgr = 0;
}


kMaster::~kMaster()
{
}

kMaster * kMaster::getInstance()
{
  static kMaster *instance;
  if ( instance ){
    _D << "instance is created"; 
    return instance;
  }
  return (instance = new kMaster());
}

int kMaster::executeApp(int argc, char *argv[])
{
// #ifdef KDE
//     KApplication app(argc, argv);
//     if (app.isRestored())
//     {
//         RESTORE(kmc);
//     } else {
//         KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
//         /// @todo do something with the command line args here
//         app.setMainWidget( mainWin );
//         args->clear();
//     }
// #else
    QApplication app(argc, argv);
// #endif
    printInfo();
    initModulesManager();
    app.setWindowIcon(QIcon(":/icons/main32.png"));
    mainWnd = new kMediaCatalog();
    mainWnd->show();
    return  app.exec();
}

int kMaster::scanDirectory(const QString & dir)
{
  newScan(dir);
}

int kMaster::newScan(const QString & cat)
{
  kMedia media;
  modmgr->startScanning();
  int r = media.newScan();
  modmgr->stopScanning();
}

bool kMaster::initModulesManager()
{
  _D << "init kModMgr";
  modmgr = new kModMgr(this);
  _D << "find modules";
  modmgr->findModules();
}

void kMaster::printInfo()
{
  cout << APP_TITLE << " version " << APP_VER << __DATE__ << ": " __TIME__ << endl;
  cout << "exe: " << kConfig::exeName().toStdString() << endl;
  cout << "dataDir(SYTEM): " << kConfig::dataDir(kConfig::pathSystem).toStdString() << endl;
  cout << "dataDir(USER): " << kConfig::dataDir(kConfig::pathUser).toStdString() << endl;
}

